<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

define('STATUS', 'status');
define('OBJ', 'obj');
define('CODE', 'code');
define('STATUS_TRUE', 1);
define('STATUS_FALSE', 0);
define('REPLY_MESSAGE', 'msg');
define ('SESSION_ERROR_CODE', 20);

	/**
	 * Extends date helper to return current time in MySQL datetime format
	 * By Dave Rogers
	 * Shinytype.com
	 *
	 * @access  public
	 * @param   string (optional)
	 * @return  string
	*/
	function mysql_datetime($date = null) {
		if (!$date) {
			// use now() instead of time() to adhere to user setting
			$date = now();
		}
		if (is_numeric($date) && strlen($date) == 10) {
			return mdate("%Y-%m-%d %H:%i:%s", $date);
		} else {
			// try to use now()
			return mdate("%Y-%m-%d %H:%i:%s", now());
		}
	}

// --------------------------------------------------------------------

	/**
	 * Take a MySQL datetime var and turn it into PHP's Unix Epoch time
	 *
	 * @access  public
	 * @param   string
	 * @return  int
	 */
	function datetime_to_unix($date) {
		if (!$date) {
			return false;
		} else {
			return date('U', strtotime($date));
		}
	}

	/**
	 * Take a date time as input and reply as date and time as 24:00:00
	 *
	 * @access  public
	 * @param   string
	 * @return  int
	 */
	function calculateEndDate($schedule) {
		$datetime = explode(" ", $schedule);
		$time = "24:00:00";
		$datearray = array();
		$datearray[] = $datetime[0];
		$datearray[] = $time;
		$enddate = implode(" ", $datearray);
		return $enddate;
	}

	/**
	 * Take a date time as input and reply as date and time as 00:00:00
	 *
	 * @access  public
	 * @param   string
	 * @return  int
	 */
	function calculateStartDate($schedule) {
		$datetime = explode(" ", $schedule);
		$time = "00:00:00";
		$datearray = array();
		$datearray[] = $datetime[0];
		$datearray[] = $time;
		$startdate = implode(" ", $datearray);
		return $startdate;
	}

	/**
	 * Check the sessionkey is valid
	 * @param string sessionKey
	 * @return boolean <true,false>
	 */
	function check_session($sessionKey) {

		$CI = & get_instance();
		$CI->db->where('sessionKey', $sessionKey);
		$sessionKeyQuery = $CI->db->get('tbl_user_session');
		if ($sessionKeyQuery->num_rows == 1) {
			return TRUE;
		} else {
			return FALSE;
		}
	}
	/**
	 * Create Captcha
	 */
	function createcaptcha() {
		$vals = array(
				'img_path' => './captcha/',
				'img_url' => base_url() . '/captcha/',
				'img_width' => 210,
				'img_height' => 40,
		);

		$cap = create_captcha($vals);
		$actcaptcha = strtolower($cap['word']);
		$data = array(
				'captcha_time' => $cap['time'],
				'ip_address' => $this->input->ip_address(),
				'word' => $actcaptcha
		);
		$query = $this->db->insert_string('captcha', $data);
		$this->db->query($query);
		return $cap['image'];
	}
	/**
	 * Captcha Check
	 * @param unknown_type $str
	 * @return boolean
	 */
	function captcha_check($str) {
		// First, delete old captchas
		$expiration = time() - 7200; // Two hour limit
		$this->db->query("DELETE FROM captcha WHERE captcha_time < " . $expiration);

		// Then see if a captcha exists:
		$sql = "SELECT COUNT(*) AS count FROM captcha WHERE word = ? AND ip_address = ? AND captcha_time > ?";
		$matchcaptcha = strtolower($_POST['Captcha']);
		$binds = array($matchcaptcha, $this->input->ip_address(), $expiration);
		$query = $this->db->query($sql, $binds);
		$row = $query->row();

		if ($row->count == 0) {
			$this->form_validation->set_message('captcha_check', 'Security code miss match. Try again');
			return FALSE;
		}
	}
	/**
	 * Form the JSON response format and send back to client
	 * @param type $respStatus
	 * @param type $respCode
	 * @param type $respObj
	 * @param type $resMessage
	 * @return type
	 */
	function responseObject($respData = NULL) {

		if (!isset($respData[STATUS]))
			$respData[STATUS] = 1;

		if (!isset($respData[CODE]))
			$respData[CODE] = 1;

		$resp = array (
				STATUS  => $respData[STATUS],
				CODE   => $respData[CODE],
		);

		if (isset($respData[OBJ]))
			$resp[OBJ] = $respData[OBJ];

		if (isset($respData[REPLY_MESSAGE]))
			$resp[REPLY_MESSAGE] = $respData[REPLY_MESSAGE];

		return $resp;
	}

	function calculate_expiry_date($start_date, $days){
		//$expiry_date = strtotime(date("Y-m-d", strtotime($date_received)). " +1 year";
		$expiry_date = strtotime ( $days , strtotime ( $start_date ) ) ;
		$newdate = date('Y-m-d H:m:s',$expiry_date);
		return $newdate;
	} 
	/**
	 * Upload Multiple Images
	 * @param  integer $width    [description]
	 * @param  integer $height   [description]
	 * @param  string  $location [description]
	 * @return [type]            [description]
	 */
	function upload_files($width=150, $height=100, $location = '/avators'){
	
		$error = array();
		$CI = & get_instance();
		$photo_path = realpath(APPPATH. '../assets/uploads'. $location);
		$photo_path_url = base_url(). 'assets/uploads' . $location;
		$conifg = array(
				'allowed_types' => 'jpg|jpeg|gif|png',
				'upload_path' => $photo_path,
				'max_size' => 2000,
				'max_width' => '650',
				'max_height' => '400'
				);
		$CI->load->library('upload', $conifg);
		
		$files = $_FILES;
		$count = count($_FILES['userfile']['name']);
		log_message('INFO', 'No of upload files : '. $count);
		$count = $count < 5 ? $count : 4;
		
		for($i=0; $i<$count; $i++)
		{
			log_message ('INFO', 'Come inside code' . $files['userfile']['name'][$i]);
			$_FILES['userfile']['name']= $files['userfile']['name'][$i];
			$_FILES['userfile']['type']= $files['userfile']['type'][$i];
			$_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i];
			$_FILES['userfile']['error']= $files['userfile']['error'][$i];
			$_FILES['userfile']['size']= $files['userfile']['size'][$i];    

			if (! $CI->upload->do_upload())
				return false;
			else {
				$image_data =$CI->upload->data();
				$result[] = $image_data['file_name'];
			}
		}
			return $result;
		}
		/**
		 * Upload Single Filedo
		 * @param  integer $width    [description]
		 * @param  integer $height   [description]
		 * @param  string  $location [description]
		 * @return [type]            [description]
		 */
		function upload_file($width=150, $height=100, $location = '/avators'){
	
		$error = array();
		$CI = & get_instance();
		$photo_path = realpath(APPPATH. '../assets/uploads'. $location);
		$photo_path_url = base_url(). 'assets/uploads' . $location;
		$conifg = array(
				'allowed_types' => 'jpg|jpeg|gif|png',
				'upload_path' => $photo_path,
				'max_size' => 2000,
				'max_width' => '650',
				'max_height' => '400'
				);
		$CI->load->library('upload', $conifg);
		
		$files = $_FILES;
		$count = count($_FILES['userfile']['name']);
		log_message('INFO', 'No of upload files : '. $count);
		$count = $count < 4 ? $count : 4;
		if ($count == 1){
			log_message('INFO', 'To upload one file');
			if (! $CI->upload->do_upload())
				return false;
			else {
				$image_data =$CI->upload->data();
				$result[] = $image_data['file_name']; ;
			}
		}
		}
		
		//$result = $photo_path . '/' . $photo_path_url;
		/**if(! $CI->upload->do_upload()){
			$result = false;
			return $result;
		}else{
			$image_data = $CI->upload->data();
			$result = $image_data['file_name'];
		}
		$config = array(
				'source_image' => $image_data['full_path'],
				'new_image' => $photo_path . '/avators',
				'maintain_ration' => true,
				'width' => $width,
				'height' => $height
				);
		
		$CI->load->library('image_lib', $config);
		$CI->image_lib->resize();
		**/
		
	
	// get automobiles total type
	function get_autos_brand_count(){
	
		$CI = & get_instance();
		$CI->load->model('Autobrand_model','',TRUE);	
		$results = $CI->Autobrand_model->get_autos_cag_total();
		return  $results['rows'];
		
	}
	//get property types total
	function get_property_type_count(){
		$CI = & get_instance();
		$CI->load->model('Propertycatg_model','',TRUE);
		$results = $CI->Propertycatg_model->get_property_type_total();
		return  $results['rows'];
			
	}
	//get classifieds categories total
	function get_classfieds_type_count(){
		$CI = & get_instance();
		$CI->load->model('Classifiedscatg_model','',TRUE);
		$results = $CI->Classifiedscatg_model->get_cls_type_total();
		return  $results['rows'];
		
	}
	


/* End of file MY_date_helper.php */
/* Location: ./application/helpers/MY_date_helper.php */
?>
